package com.hang.ad.service.impl;

import com.hang.ad.constant.CommonStatus;
import com.hang.ad.constant.Constants;
import com.hang.ad.dao.AdPlanDao;
import com.hang.ad.dao.AdUserDao;
import com.hang.ad.entity.AdPlan;
import com.hang.ad.entity.AdUser;
import com.hang.ad.exceptions.AdException;
import com.hang.ad.service.IAdPlanService;
import com.hang.ad.utils.CommonUtils;
import com.hang.ad.vo.AdPlanGetRequest;
import com.hang.ad.vo.AdPlanRequest;
import com.hang.ad.vo.AdPlanResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AdPlanServiceImpl implements IAdPlanService {
    @Autowired
    private AdUserDao adUserDao;
    @Autowired
    private AdPlanDao adPlanDao;

    @Override
    @Transactional
    public AdPlanResponse createAdPlan(AdPlanRequest request) throws AdException {
        if (!request.createValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        Optional<AdUser> user = adUserDao.findById(request.getUserId());
        if (!user.isPresent()){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }
        AdPlan oldPlan = adPlanDao.findByUserIdAndPlanName(request.getUserId(), request.getPlanName());
        if (oldPlan != null){
            throw new AdException(Constants.ErrorMsg.SAME_PLAN_NAME);
        }

        AdPlan newPlan = adPlanDao.save(new AdPlan(request.getUserId(),
                request.getPlanName(),
                CommonUtils.string2Date(request.getStartDate()),
                CommonUtils.string2Date(request.getEndDate())));


        return new AdPlanResponse(newPlan.getId(),newPlan.getPlanName());
    }

    @Override
    @Transactional
    public void deleteAdPlan(AdPlanRequest request) throws AdException {
        if (!request.deleteValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        AdPlan plan = adPlanDao.findByUserIdAndPlanName(request.getUserId(), request.getPlanName());
        if (plan == null){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }

        plan.setPlanStatus(CommonStatus.INVALID.getStatus());
        plan.setUpdateTime(new Date());

        adPlanDao.save(plan);
    }

    @Override
    public List<AdPlan> getAdPlanByIds(AdPlanGetRequest request) throws AdException {
        if (!request.validate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        return adPlanDao.findAllByIdInAndUserId(request.getIds(), request.getUserId());
    }

    @Override
    @Transactional
    public AdPlanResponse updateAdPlan(AdPlanRequest request) throws AdException {
        if (!request.updateValidate()){
            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        AdPlan plan = adPlanDao.findByIdAndUserId(request.getId(), request.getUserId());
        if (plan == null){
            throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
        }
        if (StringUtils.isNotEmpty(request.getPlanName())){
            plan.setPlanName(request.getPlanName());
        }
        if (StringUtils.isNotEmpty(request.getStartDate())){
            plan.setStartDate(CommonUtils.string2Date(request.getStartDate()));
        }
        if (StringUtils.isNotEmpty(request.getEndDate())){
            plan.setEndDate(CommonUtils.string2Date(request.getEndDate()));
        }
        plan.setUpdateTime(new Date());
        plan = adPlanDao.save(plan);

        return new AdPlanResponse(plan.getId(), plan.getPlanName());
    }
}
